#pragma once
#include "Variable.h"
class IndexVariable:
	public Variable
{
public:

	IndexVariable(Variable* sig,vector<Expr*> index):Variable(sig)
	{
		this->index=index;
	}

	~IndexVariable(void)
	{
	}

	void setindex(vector<Expr*> index){
		this->index = index;
	}

	vector<Expr*> getindex(){
		return this->index;
	}
	virtual string is(){
		return "Literal_Identificador_index";
	
	}

	virtual tipo_ts * validarSemantica(tabla_simbolos *tabla_de_tipo,tabla_simbolos *tabla_de_varaible){
		item_ts *its = tabla_de_varaible->get(this->getValor());
		if(its!=0){
			if(its->getTipo()->is().compare("ARREGLO")== 0 ){
				for(int i=0;i<(int)this->getindex().size();i++){
					tipo_ts *tts = this->getindex().at(i)->validarSemantica(tabla_de_tipo,tabla_de_varaible);
					if(tts->is()!="ENTERO"){
						stringstream out;
						out<<"Parametros no son de tipo entero"<<endl;
						throw exception(out.str().c_str());
					}
					
				}
				arreglo *arr = (arreglo*)its->getTipo();
					if(arr->getDims().size()!=this->getindex().size()){
						stringstream out;
						out<<"Cantidad de parametros es incorrecta"<<endl;
						throw exception(out.str().c_str());
					}
					//versi es registro
					if(arr->getTipo()->is().compare("REGISTRO")==0&&this->getsig()!=0){
						registro *r = (registro*)arr->getTipo();
						return this->getsig()->validarSemantica(tabla_de_tipo,r->getTs());
					}else{
						return arr->getTipo();
					}
			}else{
				stringstream out;
				out<<"Se esperaba un arreglo"<<endl;
				throw exception(out.str().c_str());
			}
		}else{
			stringstream out;
			out<<"Variable No exite"<<this->getValor()<<endl;
			throw exception(out.str().c_str());
		}
	}

private:
	vector<Expr*> index;
};

